package cn.com.b2office.struts2.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import cn.com.b2office.bean.ItemQueryParam;
import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.ItemBrand;
import cn.com.b2office.service.CategoryService;
import cn.com.b2office.service.ItemBrandService;
import cn.com.b2office.service.ItemRankService;
import cn.com.b2office.service.ItemService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author zhenggang
 * @version Jul 3, 2008 3:19:32 PM comment 二级分类下面的类别浏览页面
 */
public class CategoryItemViewAction extends ActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request = null;

	private CategoryService categoryService = null;

	private ItemService itemService = null;

	private ItemBrandService itemBrandService = null;

	private ItemRankService itemRankService = null;

	private String categoryId = null;

	private String brandId = null;

	private List<Category> categories = null;

	private List<ItemBrand> brands = null;

	private ItemBrand brand = null;// 当前选择品牌

	private List<Category> ancestries = null;// 父类

	private Category category = null;// 当前商品分类

	private List<Item> items = null;

	private String layout = "pic";// 布局方式 table/pic/text

	private String sort = "default";// 排序方式

	private int sortSeq = 1;/* 0 asc 1 desc 默认倒序 */

	private int totalNum = 0; // 总记录数

	private int pageSize = 20; // 每页显示记录数

	private int pageCount = 1; // 当前页码数 (默认为1)

	private int totalPageNum = 0; // 一共多少页

	private List<List> list = null;// 商品分类

	private List<Item> history = null;// 浏览历史

	private List<Item> rankItem = null;// 本类排行产品

	public String execute() throws Exception {
		// 左边菜单栏
		list = new ArrayList<List>();
		Map<Category, List<Category>> map = categoryService
				.listCascadeCategories();
		Iterator<Category> iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			Category ct = iterator.next();
			List<Category> childs = map.get(ct);
			List item = new ArrayList();
			item.add(ct);
			item.add(childs);
			list.add(item);
		}
		// 先判断是否是二级类，若是三级类则获得其二级类
		if (categoryId.length() == 4)
			categories = categoryService.findDirectChilds(categoryId);
		else if (categoryId.length() == 6)
			categories = categoryService.findDirectChilds(categoryId.substring(
					0, 4));
		category = categoryService.getCategory(categoryId);
		if (brandId != null && !brandId.trim().equals(""))
			brand = itemBrandService.getItemBrand(brandId);
		ancestries = categoryService.findAncestries(categoryId);

		brands = itemBrandService.findItemBrands(categoryId);

		// 查询商品
		ItemQueryParam param = new ItemQueryParam();
		param.setBrandId(brandId);
		param.setCategoryId(categoryId);
		param.setPageSize(pageSize);
		param.setPageCount(pageCount);
		param.setSortFactor(sort);
		param.setSortSeq(sortSeq);
		items = itemService.queryItems(param);

		totalNum = itemService.size(param);

		totalPageNum = ((Double) Math.ceil((double) totalNum
				/ param.getPageSize())).intValue();

		history = itemService.findItemsByIterate(getHistoryRecords());

		rankItem = itemRankService.getRank(categoryId, 6);

		return SUCCESS;
	}

	/**
	 * 获取最近浏览记录
	 * 
	 * @return
	 */
	private List<String> getHistoryRecords() {
		List<String> itemIdList = new ArrayList<String>();
		Cookie[] cookies = request.getCookies();
		if (cookies == null || cookies.length == 0) {
			return itemIdList;
		}
		Cookie c = null;
		for (Cookie ck : cookies) {
			if (ck.getName().equals(ItemViewAction.COOKIE_NAME)) {
				c = ck;
				break;
			}
		}

		if (c == null || c.getValue() == null || c.getValue().trim().equals(""))
			return itemIdList;
		String[] values = c.getValue().split("&");
		for (int i = 0; i < values.length; i++) {
			itemIdList.add(values[i]);
		}
		return itemIdList;

	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<Category> getAncestries() {
		return ancestries;
	}

	public ItemBrand getBrand() {
		return brand;
	}

	public List<ItemBrand> getBrands() {
		return brands;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public Category getCategory() {
		return category;
	}

	public List<Item> getItems() {
		return items;
	}

	public List<List> getList() {
		return list;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setItemBrandService(ItemBrandService itemBrandService) {
		this.itemBrandService = itemBrandService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public void setItemRankService(ItemRankService itemRankService) {
		this.itemRankService = itemRankService;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<Item> getHistory() {
		return history;
	}

	public int getSortSeq() {
		return sortSeq;
	}

	public void setSortSeq(int sortSeq) {
		this.sortSeq = sortSeq;
	}

	public List<Item> getRankItem() {
		return rankItem;
	}

}
